/*	queue.c

	Implementation of a FIFO queue abstract data type.

	by: Steven Skiena
	begun: March 27, 2002
*/
#include "queue.h"

void queue_init(queue *q) {
        q->first = 0;
        q->last = QUEUESIZE-1;
        q->count = 0;
}

char queue_empty(queue *q) {
        if (q->count <= 0) return (PASS);
        else return (FAIL);
}

char queue_add(queue *q, char x) {
        if (q->count >= QUEUESIZE) return FAIL;
        else {
                q->last = (q->last+1) % QUEUESIZE;
                q->q[ q->last ] = x;    
                q->count = q->count + 1;
        }

		return PASS;
}

char queue_remove(queue *q) {
   		unsigned char x;

        if (q->count <= 0) return FAIL;
        else {
                x = q->q[ q->first ];
                q->first = (q->first+1) % QUEUESIZE;
                q->count = q->count - 1;
        }

        return x;
}
